//
//  ActivateManager.m
//  JianChaRiBao
//
//  Created by 常兴宇 on 13-11-28.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "ActivateManager.h"


ActivateManager * activateManager = nil;
@implementation ActivateManager
+ (ActivateManager *)shareManager{
    if (activateManager == nil) {
        activateManager = [[ActivateManager alloc]init];
    }
    return activateManager;
}

- (void) UUIDIsActivated{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       id obj = [[DataRequest sharedRequest] getUUIDInfo:UUID];
        NSLog(@"obj:%@",obj);
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([obj isKindOfClass:[NSDictionary class]] && [[obj objectForKey:@"result"]isKindOfClass:[NSArray class]]) {
                NSDictionary * dict = [[obj objectForKey:@"result"] objectAtIndex:0];
                if ([[dict objectForKey:@"status_code"]integerValue] == 0) {
                    if (self.isReview) {
                        self.isActivated = NO;
                    }

                    [[NSNotificationCenter defaultCenter] postNotificationName:@"ActivateStateChanged" object:nil];
                  
                    
                    NSTimeZone *zone = [NSTimeZone systemTimeZone];
                    NSDate *date = [NSDate date];
                    NSInteger interval = [zone secondsFromGMTForDate: date];
                    NSDate *currDate = [date dateByAddingTimeInterval:interval];
                   
                    NSString * dateStr = @"2014-01-01";
                    NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
                    [inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"zn_CH"] autorelease]];
                    [inputFormatter setDateFormat:@"yyyy-MM-dd"];
                    NSDate* inputDate = [inputFormatter dateFromString:dateStr];
                    NSDate *validDate = [inputDate  dateByAddingTimeInterval: interval];

                    NSComparisonResult result = [validDate compare:currDate];
                    
                    NSLog(@" date: %@", validDate);
                    
                    NSLog(@"date = %@", currDate);
                    

                    if (result == NSOrderedAscending) {
                        NSLog(@"免费 已过期");
                    }else if (result == NSOrderedDescending){
                        NSLog(@"免费 未过期");
                        self.isFree = YES;
                    }else{
                        NSLog(@"免费 同一天");
                        self.isFree = YES;
                    }

                    
                }else{
                    self.isActivated = YES;
                    [[NSUserDefaults standardUserDefaults] setObject:[dict objectForKey:@"end_time"] forKey:EndTime];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"ActivateStateChanged" object:nil];
                }
            }

        });
    });

}
- (void) activateWith:(NSString *)num{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        id obj = [[DataRequest sharedRequest] getActivateInfoUUID:UUID activateNum:num];
        NSLog(@"obj:%@",obj);
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([obj isKindOfClass:[NSDictionary class]] && [[obj objectForKey:@"result"]isKindOfClass:[NSArray class]]) {
                ABPromptView * abp = [[ABPromptView alloc]initWithTitle:[[[obj objectForKey:@"result"] objectAtIndex:0] objectForKey:@"status_message"]];
                [abp show];
                [abp release];
                if ([[[[obj objectForKey:@"result"] objectAtIndex:0] objectForKey:@"status_code"] integerValue] == 5) {
                    self.isActivated = YES;
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"ActivateStateChanged" object:nil];
                    [[NSUserDefaults standardUserDefaults] setObject:[[[obj objectForKey:@"result"] objectAtIndex:0] objectForKey:@"end_time"] forKey:EndTime];
                    
                }
            }
        });
    });

}
- (void)setData{
    
    NSString * end_time = [[NSUserDefaults standardUserDefaults] objectForKey:EndTime];
    NSLog(@"end_time:%@",end_time);
    if (end_time != nil && ![end_time isEqualToString:@""]) {
        NSDate *date = [NSDate date];
        NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"zn_CH"] autorelease]];
        [inputFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate* inputDate = [inputFormatter dateFromString:end_time];

        //设置时区
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate: date];
        //有效期
        NSDate *validDate = [inputDate  dateByAddingTimeInterval: interval];
        //当前时间
        NSDate *currDate = [date dateByAddingTimeInterval:interval];
        
        NSLog(@" date: %@", validDate);

        NSLog(@"date = %@", currDate);
        
        
        //        NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
        //
        //        [dateformatter setDateFormat:@"YYYY-MM-dd"];
        //
        //        NSString *  locationString=[dateformatter stringFromDate:senddate];
        //
        //        NSLog(@"locationString:%@",locationString);
        //
        //        [dateformatter release];
        NSComparisonResult result = [validDate compare:currDate];
        if (result == NSOrderedAscending) {
            NSLog(@"已过期");
            //当激活码已过期的时候
            self.isActivated = NO;
            [[NSUserDefaults standardUserDefaults] setObject:nil forKey:EndTime];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            ABPromptView *abp = [[[ABPromptView alloc]initWithTitle:@"激活码已过期"]autorelease];
            [abp show];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ActivateStateChanged" object:nil];
            
        }else if (result == NSOrderedDescending){
            NSLog(@"未过期");
            self.isActivated = YES;
        }else{
            NSLog(@"同一天");
            self.isActivated = YES;
        }
        
//        [senddate timeIntervalSinceDate:inputDate]
    }else{
        [self UUIDIsActivated];
    }

}
-(void)getReviewStatus{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Reviewed"]) {
        self.isReview = YES;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ReviewStatus" object:nil];
        return;
    }
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        id obj = [[DataRequest sharedRequest] getReviewStatus];
        //        {"result":{"status_code":1}}
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([obj isKindOfClass:[NSDictionary class]] && [[obj objectForKey:@"result"] isKindOfClass:[NSDictionary class]]) {
                NSDictionary *dict = [obj objectForKey:@"result"];
                if ([[dict objectForKey:@"status_code"] boolValue]) {
                    NSLog(@"1111111111111");
                    self.isReview = YES;
                    [[NSUserDefaults standardUserDefaults] setBool:self.isReview forKey:@"Reviewed"];
                    
                }else{
                    NSLog(@"2222222222222");
                    self.isActivated = YES;
                    self.isReview = NO;
                }
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ReviewStatus" object:nil];

            }
        });
        
    });
    //得到审核状态
}


@end
